package me.aartikov.alligator;


import me.aartikov.alligator.destinations.AbilityDestination;
import me.aartikov.alligator.destinations.Destination;
import me.aartikov.alligator.listeners.ScreenResultListener;
import me.aartikov.alligator.navigationfactories.NavigationFactory;
import me.aartikov.alligator.helpers.ScreenResultHelper;
import ohos.aafwk.content.Intent;

/**
 * Helper class for handling a screen result.
 */
public class AbilityResultHandler {
    private NavigationFactory mNavigationFactory;
    private ScreenResultListener mScreenResultListener;
    private ScreenResultPair mPendingScreenResultPair;

    AbilityResultHandler(NavigationFactory navigationFactory) {
        mNavigationFactory = navigationFactory;
    }

    void setScreenResultListener(ScreenResultListener screenResultListener) {
        mScreenResultListener = screenResultListener;
        handlePendingScreenResult();
    }

    void resetScreenResultListener() {
        mScreenResultListener = null;
    }

    /**
     * Handles activity result. This method should be called from {@code onActivityResult} of an activity.
     *
     * @param requestCode requestCode passed to {@code onActivityResult}
     * @param resultCode  resultCode passed to {@code onActivityResult}
     * @param data        intent passed to {@code onActivityResult}
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Class<? extends Screen> screenClass = mNavigationFactory.getScreenClass(requestCode);
        if (screenClass != null) {
            Destination destination = mNavigationFactory.getDestination(screenClass);
            if (destination instanceof AbilityDestination) {
                ScreenResult screenResult = ((AbilityDestination) destination).getScreenResult(new AbilityResult(resultCode, data));
                if (mPendingScreenResultPair == null || mPendingScreenResultPair.mScreenResult == null) {
                    mPendingScreenResultPair = new ScreenResultPair(screenClass, screenResult);
                }
                handlePendingScreenResult();
            }
        }
    }

    /**
     * Handles activity result. This method should be called from {@code onNewIntent} of an activity.
     *
     * @param intent intent passed to {@code onNewIntent}
     */
    public void onNewIntent(Intent intent) {
        int requestCode = intent.getIntParam(ScreenResultHelper.KEY_REQUEST_CODE, -1);
        if (requestCode != -1) {
            int resultCode = intent.getIntParam(ScreenResultHelper.KEY_RESULT_CODE, 0);
            onActivityResult(requestCode, resultCode, intent);
        }


// 		int requestCode = intent.getIntExtra(ScreenResultHelper.KEY_REQUEST_CODE, -1);
//		if (requestCode != -1) {
//			int resultCode = intent.getIntExtra(ScreenResultHelper.KEY_RESULT_CODE, 0);
//			onActivityResult(requestCode, resultCode, intent);
//		}
    }

    private void handlePendingScreenResult() {
        if (mScreenResultListener != null && mPendingScreenResultPair != null) {
            mScreenResultListener.onScreenResult(mPendingScreenResultPair.mScreenClass, mPendingScreenResultPair.mScreenResult);
            mPendingScreenResultPair = null;
        }
    }

    private static class ScreenResultPair {
        Class<? extends me.aartikov.alligator.Screen> mScreenClass;
        me.aartikov.alligator.ScreenResult mScreenResult;

         ScreenResultPair(Class<? extends Screen> screenClass, ScreenResult screenResult) {
            mScreenClass = screenClass;
            mScreenResult = screenResult;
        }
    }
}